/** 
 *  Copyright (c) 2013  Ng Pan Wei
 *  
 *  Permission is hereby granted, free of charge, to any person 
 *  obtaining a copy of this software and associated documentation files 
 *  (the "Software"), to deal in the Software without restriction, 
 *  including without limitation the rights to use, copy, modify, merge, 
 *  publish, distribute, sublicense, and/or sell copies of the Software, 
 *  and to permit persons to whom the Software is furnished to do so, 
 *  subject to the following conditions: 
 *  
 *  The above copyright notice and this permission notice shall be 
 *  included in all copies or substantial portions of the Software. 
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
 *  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
 *  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 *  SOFTWARE. 
 */

package jFeature.ant;

import jFeature.analysis.Analysis;
import jFeature.analysis.ClassScanner;
import jFeature.reporting.ReportVisitor;
import jFeature.reporting.Reporter;
import jFeature.reporting.TextReportVisitor;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;

public class FeatureTask extends Task {
	String featurePackage ;
	String classPackage ;
	String message;

	public void execute() throws BuildException {
		try {
			log("Feature Package: " + featurePackage, Project.MSG_INFO);
			log("Class Package: " + classPackage, Project.MSG_INFO);
			Analysis analysis = new Analysis() ;
			getClassLoader() ;
			analysis.read(featurePackage.split(","), classPackage.split(",")) ;
			analysis.analyze() ;
			
			Reporter reporter = new Reporter();
			String report = "" ;
			ReportVisitor visitor = null ;

			visitor = new TextReportVisitor() ;
			report = reporter.generateReport(analysis, visitor) ;
			System.err.println(report) ;			
		} catch (Exception e) {
			throw new BuildException(e) ;
		}
	}

	public void setMessage(String message) {
		this.message = message;
	}
	public void setFeaturePackage(String value) {
		featurePackage = value ;
	}
	public void setClassPackage(String value) {
		classPackage = value ;
	}
	
	public void getClassLoader() {
	    ClassLoader loader = this.getClass().getClassLoader();
	    ClassScanner.setClassloader(loader) ;
	}

}
